<?php

namespace app\storeapi\controller;

use app\storeapi\model\Shop as ShopModel;
use app\storeapi\model\Order as OrderModel;
use app\storeapi\model\User as UserModel;
use app\storeapi\model\Wxapp as WxappModel;
use app\storeapi\validate\Store as StoreValidate;

/**
 * 商户主页
 * Class Index
 * @package app\storeapi
 */
class Index extends Controller
{

    /**
     * 商户主页
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data = (new ShopModel)->index($this->wxapp_id);
        if(!$data){
            return $this->renderError('暂无门店',[]);
        }
        return $this->renderSuccess($data);

    }

    /**
     * 我的门店
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function shop()
    {
        $param = $this->postData();
        $shop_id = $param['shop_id']?:0;
        $data = (new ShopModel)->shop($shop_id)?:[];
        if(!$data){
            return $this->renderError('暂无门店',[]);
        }
        return $this->renderSuccess($data);

    }



    /**
     * 店铺信息编辑
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function editInfo()
    {
        //获取数据
        $param = $this->postData();
        $validate = new StoreValidate;
        // 表单验证
        if (!$validate->scene('editInfo')->check($param)) {
            return $this->renderError($validate->getError());
        }
        $data = (new ShopModel)->editInfo($param);
        if($data){
            return $this->renderSuccess([],'编辑成功');
        }else{
            return $this->renderError('编辑失败');
        }

    }

    /**
     * 我的资金
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function storeMoney()
    {
        $param = $this->postData();
        $model = new OrderModel;
        return $this->renderSuccess([
            'todayMoney' => $model::todayMoney($param),
            'allMoney' => $model::allMoney($param),
            'logMoney' => $model::logMoney($param),
        ]);
    }

    /**
     * 收款记录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function getMoney()
    {
        $param = $this->postData();
        $model = new OrderModel;
        return $this->renderSuccess($model::logGetMoney($param));

    }

    /**
     * 经营报表
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function reportFrom()
    {
        $param = $this->postData();
        $model = new OrderModel;
        return $this->renderSuccess($model::reportFrom($param));

    }

    /**
     * 用户列表
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function userList()
    {
        $param = $this->postData();
        $model = new UserModel;
        $list = $model->userList($param);
        return $this->renderSuccess($list);
    }

    /**
     * 收款码
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function code()
    {
        $param = $this->postData();
        $model = new WxappModel;
        $list = $model->code($param['wxapp_id']);
        return $this->renderSuccess($list);
    }


}
